package com.apes.scm.masterdata.party.repository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.masterdata.party.model.AccessProvider;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;


public interface AccessProviderRepository extends CustomRepository<AccessProvider, String> {

//    @Query("select T from AccessProvider T where 1 = 1 and lastTimestamp > :lastTimestamp ORDER BY id,lastTimestamp ASC")
//    List<AccessProvider> findByLastUpdatedStamp(@Param("lastTimestamp") Date lastTimestamp);
//
    @Query("select T from AccessProvider T where lastUpdatedStamp > :times or (id > :id and lastUpdatedStamp = :times) order by lastUpdatedStamp, id asc ")
    List<AccessProvider> findByLastUpdatedStampAndId(@Param("times") Date times, @Param("id") String id, Pageable pageable);

//    @Query("select new map(s.id as id, s.product.id as productId, s.product.name as productName," +
//            " s.location.depot.id as depotId, s.location.depot.id as depotName, " +
//            " s.location.id as locationId, s.location.name as locationName, " +
//            " s.company.id as companyId, s.company.party.name as companyName, " +
//            " s.usableQty as usableQty, s.updateDate as updateDate, " +
//            " to_char(s.lastUpdatedStamp, 'yyyy-mm-dd hh24:mi:ss.FF3') as lastUpdatedStamp ) " +
//            " FROM AccessProvider s " +
//            " where s.lastUpdatedStamp > :times or (s.id > :id and s.lastUpdatedStamp = :times)  " +
//            " order by s.lastUpdatedStamp, s.id asc ")
//    List<Map> findByLastUpdatedStampAndId(@Param("lastTimestamp") Date lastTimestamp, @Param("id") String id, @Param("maxNum") int maxNum);

    @Query(value = "select t.* from mas_access_provider t where t.id in (select T.access_id from s_access_company T where t.company_id = :companyId) ", nativeQuery = true)
    AccessProvider findByCompany(@Param("companyId") String companyId);
}
